/**
 * Created by momomo on 2017/3/20.
 */
var leftTime = 0;
var seckillId = parseInt($("#seckillId").val());


function freshKillTime(){
    console.log("freshKillTime");
    leftTime = leftTime - 1;
    var str = "0天0小时0分0秒";
    if(leftTime > 0) {
        str = getTimerString(leftTime);
    }
    $("#timeOut").html(str);
    if(leftTime > 0) {
        setTimeout("freshKillTime()", 1000);
    }else{
        killTimeStart();
    }
}


function getTimerString(time) {
    var days = Math.floor(time/86400);
    time = time - days * 86400;
    var hours = Math.floor(time/3600);
    time = time - hours * 3600;
    var minutes= Math.floor(time/60);
    time = time - minutes * 60;
    var seconds = Math.floor(time);
    return days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
}


function killTimeStart() {
    $.ajax({
        type: 'GET',
        url: '/seckill/getkillentry/'+seckillId,
        data: {},
        async: false,
        success: function (data) {
            var isAvailable = data['available'];
            if(!isAvailable) {
                leftTime = parseInt(data['leftTime']);
                if(leftTime == -1){
                    alert("无效抢购");
                    location.href = "/";
                } else {
                    setTimeout("freshKillTime()", 1000);
                }
            }else{
                var btn = $("#kill");
                btn.removeAttr("disabled");
                btn.click(function(){
                    location.href = "/seckill/dokill/"+seckillId+"/"+data['md5'];
                    btn.attr("disabled","disabled");
                    btn.css("background","grey");
                });
                btn.css("background","yellow");
                btn.css("color","black");
                $("#timeOut").html("秒杀时间到");
            }
        },
    });
}

killTimeStart();



